'use strict'

define(['jquery', 'qiniuPlayer', 'datetimepicker', 'datetimepickerCN', 'bootstrap', 'select2', 'shiftform',
    'validate', 'validate.en', 'plupload', 'qiniu'], function($, QPlayer) {
  return {
    init: function() {
      this.initCreate();
      this.initOpen();
      this.initClose();
      this.initSearch();
      this.initCoverEdit();
    },

    initCreate: function() {
    	var that = this;
      $(document).on('shown.bs.modal', '#create', function(e) {
      	
      	that.uploadCover();
      	
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        // validate
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },
    
    initOpen: function() {
      $(document).on('shown.bs.modal', '#open', function(e) {
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        // validate
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },
    
    initClose: function() {
      $(document).on('shown.bs.modal', '#close', function(e) {
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        // validate
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },
    
    initCoverEdit: function() {
      var that = this;
      $(document).on('shown.bs.modal', '#edit-cover', function(e) {
        
        that.player = new QPlayer({
          url: "http://aipunet.qiniu.edooschool.com/Delegate-System-Tutorial.mp4",
          container: document.getElementById("videoPlayer"),
          defaultViewConfig: {
            showControls: true,
          }
        });
        
        that.uploadCover();
        
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        // validate
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },
    
    uploadCover: function() {
      
      // 初始化页面组件
      var imgSrc = $(".upload-cover").find("img").attr("src");
      if(imgSrc == null || imgSrc == undefined || imgSrc == "") {
        $(".upload-cover").find("img").addClass("hide");
      }
      
      $(".upload-cover").hover(function(){
        $(".upload-cover .edit-btn").addClass("in");
      },function(){
        $(".upload-cover .edit-btn").removeClass("in");
      });
      
      // 请求qiniu token
      $.ajax({
        
        url: window.__CTX__.cloudUptoken,
        // The name of the callback parameter, as specified by the YQL service
        jsonp: "callback",
        // Tell jQuery we're expecting JSONP
        dataType: "jsonp",
        // Work with the response
        success: function( response ) {
          var token = response.token;
          
          // 执行上传
          var uploaderCover = Qiniu.uploader({
            runtimes: 'html5,flash,html4',
            browse_button: 'pickCover',
            uptoken: token,
            domain: 'http://upload.qiniu.com/',
            max_file_size: '4mb',
            max_retries: 3,
            chunk_size: '2mb',
            save_key: true,
            auto_start: true,
            init: {
              'UploadProgress': function(up, file) {
                $('#messageCover').removeClass("hide");
                $(".edit-btn").addClass("hide").prop("disabled", true);
                $('#messageCover').html("正在上传图片，请稍后");
                $('#submitBtn').attr("disabled", "disabled");
              },
              'FileUploaded': function(up, file, info) {
                
                // 将上传url地址放入form表单
                var res = $.parseJSON(info);
                var cover_box = '';
                $('#coverPreview').attr("src", window.__CTX__.cloudProd + "/" + res.key + "-0.6").removeClass("hide");;
                $('#coverPreviewList').removeClass("hide");
                $('#cover').val(res.key);
                $('#messageCover').html("加载图片完成");
                setTimeout(function() {
                  $('#messageCover').addClass("hide");
                  $(".edit-btn").addClass("fade").removeClass("hide").prop("disabled", false).html("<span class='glyphicon glyphicon-repeat' aria-hidden='true'></span>&nbsp;&nbsp;重新上传");
                }, 2000);
                $('#submitBtn').removeAttr('disabled');
              },
              'Error': function(up, err, errTip) {
                $('#messageCover').html("图片上传异常，请稍后重试");
              }
            }
          });
        }
      })
      
    },
    
    initSearch: function() {
      $(document).on("shown.bs.modal", "#search", function(e) {
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate() //bind validate
      });
    }
  }
})
